package com.ruoyi.management_controller.liaison_station.StreetInfo.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.management_controller.PublicOpinion.domain.PublicOpinion;
import com.ruoyi.management_controller.liaison_station.LiaisonStation.domain.LiaisonStation;
import com.ruoyi.management_controller.liaison_station.LiaisonStation.mapper.LiaisonStationMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.management_controller.liaison_station.StreetInfo.mapper.StreetInfoMapper;
import com.ruoyi.management_controller.liaison_station.StreetInfo.domain.StreetInfo;
import com.ruoyi.management_controller.liaison_station.StreetInfo.service.IStreetInfoService;

/**
 * 街道信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-07-09
 */
@Service
public class StreetInfoServiceImpl implements IStreetInfoService 
{
    @Autowired
    private StreetInfoMapper streetInfoMapper;

    @Autowired
    private LiaisonStationMapper liaisonStationMapper;

    /**
     * 查询街道信息
     * 
     * @param id 街道信息主键
     * @return 街道信息
     */
    @Override
    public StreetInfo selectStreetInfoById(String id)
    {
        return streetInfoMapper.selectStreetInfoById(id);
    }

    /**
     * 查询街道信息列表
     * 
     * @param streetInfo 街道信息
     * @return 街道信息
     */
    @Override
    public List<StreetInfo> selectStreetInfoList(StreetInfo streetInfo)
    {
        List<StreetInfo> streetInfos=streetInfoMapper.selectStreetInfoList(streetInfo);
        for(StreetInfo s:streetInfos){
            StreetInfo s1= getStreeByStreeid(s.getId());
            s.setStationCount(s1.getStationCount());
            s.setRepresentativesCount(s1.getRepresentativesCount());
            s.setStreetName(s1.getStreetName());
        }
        return streetInfos;
    }


    /**
     * 新增街道信息
     * 
     * @param streetInfo 街道信息
     * @return 结果
     */
    @Override
    public int insertStreetInfo(StreetInfo streetInfo)
    {
        streetInfo.setId(StreetInfo.getDefaultId());
        return streetInfoMapper.insertStreetInfo(streetInfo);
    }

    /**
     * 修改街道信息
     * 
     * @param streetInfo 街道信息
     * @return 结果
     */
    @Override
    public int updateStreetInfo(StreetInfo streetInfo)
    {

        return streetInfoMapper.updateStreetInfo(streetInfo);
    }

    /**
     * 批量删除街道信息
     * 
     * @param ids 需要删除的街道信息主键
     * @return 结果
     */
    @Override
    public int deleteStreetInfoByIds(String[] ids)
    {
        return streetInfoMapper.deleteStreetInfoByIds(ids);
    }

    /**
     * 删除街道信息信息
     * 
     * @param id 街道信息主键
     * @return 结果
     */
    @Override
    public int deleteStreetInfoById(String id)
    {
        return streetInfoMapper.deleteStreetInfoById(id);
    }


    public StreetInfo getStreeByStreeid(String id){
        List<LiaisonStation> lias=liaisonStationMapper.selectfbyStreetid(id);
        StreetInfo streetInfo=streetInfoMapper.selectStreetInfoById(id);
        if(lias.size()>0) streetInfo.setStationCount(lias.stream().count());
        long count=0;
        if(lias.size()>0){
            for (LiaisonStation l: lias){
                count+=l.getRepresentativesCount();
            }
        }
        streetInfo.setRepresentativesCount(count);
        return streetInfo;
    };

//    public StreetInfo getStreeByStreeid1(PublicOpinion publicOpinion){
//        StreetInfo streetInfo=streetInfoMapper.selectStreetInfoById(publicOpinion.get);
//        if(lias.size()>0) streetInfo.setStationCount(lias.stream().count());
//        long count=0;
//        if(lias.size()>0){
//            for (LiaisonStation l: lias){
//                count+=l.getRepresentativesCount();
//            }
//        }
//        streetInfo.setRepresentativesCount(count);
//        return streetInfo;
//    };
}
